package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthDomainAim;
import orm.CollectivityWeekTimeBucket;
import orm.IndividualWeekDomainAim;
import util.DBHelper;

public class IWeekDomainAimDAO {
	
	public void insertIWeekDomainAim(Statement statement, 
			ArrayList<IndividualWeekDomainAim> iwdaArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekDomainAim iwda = null;
		for (int i = 0; i < iwdaArr.size(); i++) {
			iwda = iwdaArr.get(i);
			statement.execute("insert into individualweekdomainaim values('" + iWeekArrangeID + "'," +
					iwda.getDomainID() + ", " + iwda.getWeekAimID() + ", '" + iwda.getWeekAimDescribe() + "')");
		}
	}
	
	public void updateIWeekDomainAim(Statement statement,
			ArrayList<IndividualWeekDomainAim> iwdaArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekDomainAim iwda = null;
		for (int i = 0; i < iwdaArr.size(); i++) {
			iwda = iwdaArr.get(i);
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update individualweekdomainaim set WeekAimDescribe = '" + iwda.getWeekAimDescribe() + "'");
			sqlStr.append("where IWeekArrangeID = '");
			sqlStr.append(iwda.getIWeekArrangeID() + "' and DomainID = " + iwda.getDomainID() + " and WeekAimID = " + iwda.getWeekAimID() + "");
			
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIWeekDomainAim(Statement statement, String iWeekArrangeID) throws SQLException {
		statement.execute("delete from individualweekdomainaim where IWeekArrangeID='" + iWeekArrangeID + "'");
	}
	
	public ArrayList<IndividualWeekDomainAim> getiwdaArrByCWAID(String iWeekArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IndividualWeekDomainAim> al = new ArrayList<IndividualWeekDomainAim>();
		IndividualWeekDomainAim iwda = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from individualweekdomainaim where IWeekArrangeID='" + iWeekArrangeID + "'");
		
		while (rs.next()) {
			iwda = new IndividualWeekDomainAim();
			int i = 1;
			iwda.setIWeekArrangeID(rs.getString(i++));
			iwda.setDomainID(rs.getInt(i++));
			iwda.setWeekAimID(rs.getInt(i++));
			iwda.setWeekAimDescribe(rs.getString(i++));
			al.add(iwda);
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
